package net.dopan.pigframe.oss.storagecloud;

import com.xiaoleilu.hutool.util.StrUtil;
import net.dopan.mytools.upload.fastdfs.client.FastDFSClient;
import net.dopan.mytools.upload.fastdfs.client.FastDFSException;
import net.dopan.pigframe.core.utils.RedisUtils;
import net.dopan.pigframe.core.utils.SysConfigConstant;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Package: net.dopan.pigframe.oss.storagecloud
 * @Title: FastDFSStorageService
 * @Description: FastDFS存储
 * @author: 刘宽
 * @date: 2018-08-16 10:24
 */
public class FastDFSStorageService extends AbstractCloudStorageService {

    private String baseUrl = "";

    public FastDFSStorageService(CloudStorageConfig config) {
        this.config = config;

        //初始化
        init();
    }

    private void init(){
        if(StrUtil.isBlankIfStr(RedisUtils.get(SysConfigConstant.CLOUD_STORAGE_OSS_CONFIG + ":" + SysConfigConstant.CLOUD_STORAGE_FASTDFS_URL)))
        {
            System.out.println("------配置文件读取开始");
            InputStream temp = getClass().getClassLoader().getResourceAsStream("config.properties");
            if (null != temp) {
                Properties prop = new Properties();
                try {
                    prop.load(temp);
                    this.baseUrl = prop.getProperty("upload.file_server_addr");
                    RedisUtils.set(SysConfigConstant.CLOUD_STORAGE_OSS_CONFIG + ":" + SysConfigConstant.CLOUD_STORAGE_FASTDFS_URL, baseUrl);
                    System.out.println("-----配置文件读取结束");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        else {
            this.baseUrl = RedisUtils.get(SysConfigConstant.CLOUD_STORAGE_OSS_CONFIG + ":" + SysConfigConstant.CLOUD_STORAGE_FASTDFS_URL).toString();
        }
    }

    @Override
    public String upload(byte[] data, String path, String fileName) {
        return upload(new ByteArrayInputStream(data), path, fileName);
    }

    @Override
    public String upload(InputStream inputStream, String path, String fileName) {
        try {
            Map<String, String> descriptions = new HashMap<String, String>(3);
            descriptions.put("name",fileName);
            descriptions.put("size",String.valueOf(inputStream.available()/1000) + "K");
            descriptions.put("time",new Date().toString());
            return baseUrl + "/"+ FastDFSClient.upload(inputStream, fileName, descriptions);
        } catch (FastDFSException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String uploadSuffix(byte[] data, String suffix, String fileName) {
        return upload(data, getPath("fastdfs", suffix), fileName);
    }

    @Override
    public String uploadSuffix(InputStream inputStream, String suffix, String fileName) {
        return upload(inputStream, getPath("fastdfs", suffix), fileName);
    }
}